package com.sinopec.basemodule.dialog;

import android.content.Context;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.sinopec.basemodule.R;
import com.sinopec.basemodule.adapter.CommonAdapter;
import com.sinopec.basemodule.adapter.CustomViewHolder;
import com.sinopec.basemodule.model.ProductBusinessNameItemModel;
import com.sinopec.basemodule.widget.popupwindow.BasePopup;

import java.util.List;

/**
 * @author lwj
 * @ClassName: ChooseProductBusinessNamePopupWindow
 * @Description: 选择业务类型弹窗
 */
public class ChooseProductBusinessNamePopupWindow extends BasePopup<ChooseProductBusinessNamePopupWindow> {
    private Context mContext;
    private OnSelectListener listener;
    private RecyclerView rv_list;
    private List<ProductBusinessNameItemModel> models;
    //当前项目
    private String curCode;
    private CommonAdapter<ProductBusinessNameItemModel> adapter;
    //关闭
    private ImageView iv_close;

    public static ChooseProductBusinessNamePopupWindow create(Context context, List<ProductBusinessNameItemModel> models, String curCode, OnSelectListener listener) {
        return new ChooseProductBusinessNamePopupWindow(context, models, curCode, listener);
    }

    public ChooseProductBusinessNamePopupWindow(Context context, List<ProductBusinessNameItemModel> models, String curCode, OnSelectListener listener) {
        this.listener = listener;
        this.mContext = context;
        this.models = models;
        this.curCode=curCode;
        setContext(context);
    }


    @Override
    protected void initAttributes() {
        setContentView(R.layout.choose_product_name_popupwindow);
        DisplayMetrics d = mContext.getResources().getDisplayMetrics();
        //设置弹窗宽高比例IntEKt.dp2px(370)
        this.setWidth((int) (d.widthPixels * 0.20));
        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);

        setFocusAndOutsideEnable(true)
                .setBackgroundDimEnable(true)
                .setNeedReMeasureWH(true)
                .setDimValue(0f);

//        setContentView(R.layout.choose_warehouse_popupwindow, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
//        setFocusAndOutsideEnable(true)
//                .setBackgroundDimEnable(true)
//                .setNeedReMeasureWH(true)
//                .setDimValue(0.5f);
    }

    @Override
    protected void initViews(View view, ChooseProductBusinessNamePopupWindow basePopup) {
        rv_list = view.findViewById(R.id.rv_list);
        adapter = new CommonAdapter<ProductBusinessNameItemModel>(mContext, R.layout.product_name_item_popuwindow, models) {
            @Override
            public void convert(CustomViewHolder holder, ProductBusinessNameItemModel model, int position) {
                View itemView = holder.itemView;
                //名称
                TextView tv_name = holder.getView(R.id.tv_name);
                tv_name.setText(model.getProductBusinessName());
                itemView.setOnClickListener(v -> {
                    listener.onSelect(model);
                    dismiss();
                });
                View v_line = holder.getView(R.id.v_line);
                if(models.size() == Integer.parseInt((position + 1) + "")){
                    v_line.setVisibility(View.GONE);
                }else{
                    v_line.setVisibility(View.VISIBLE);
                }
            }
        };
        rv_list.setAdapter(adapter);

        //关闭弹窗
//        iv_close = view.findViewById(R.id.iv_close);
//        iv_close.setOnClickListener((View view1) -> {
//            dismiss();
//        });

    }

    public interface OnSelectListener {
        /**
         * 选择项
         */
        void onSelect(ProductBusinessNameItemModel model);
    }
}
